<template>
	<view class="container">
		<!-- 输入框区域 -->
		<view class="input-box">
			<textarea 
				class="textarea" 
				placeholder="请填写" 
				v-model="content"
				maxlength="500"
			/>
		</view>
		
		<!-- 提交按钮 -->
		<button class="submit-btn" @click="sendAdvice">提交</button>
	</view>
</template>

<script>
import { postAdvice } from "../../api/index.js"
	export default {
		data() {
			return {
				content: ''
			}
		},
		methods: {
			sendAdvice() {
				// 先获取用户信息
				const userInfo = uni.getStorageSync('userInfo')
				
				// 检查用户信息是否存在
				if (!userInfo || !userInfo.id) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					})
					// 可以选择跳转到登录页
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 1500)
					return
				}

				if (this.content.trim()) {
					postAdvice({
						content: this.content,
						users_id: userInfo.id
					}).then(res => {
						console.log(res)    
						if(res.data.status === 0){
							uni.showToast({
								title: '提交成功',
								icon: 'none'
							})
							// 提交成功后清空内容
							this.content = ''
						}else{
							uni.showToast({
								title: res.data.message || '提交失败',
								icon: 'none'
							})
						}
					})
				} else {
					uni.showToast({
						title: '输入不能为空',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style>
	.container {
		padding: 20rpx;
	}

	.input-box {
		background-color: #FFFFFF;
		border-radius: 12rpx;
		padding: 20rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
	}

	.textarea {
		width: 100%;
		height: 300rpx;
		font-size: 28rpx;
	}

	.submit-btn {
		background-color: #007AFF;
		color: #FFFFFF;
		border-radius: 8rpx;
		font-size: 32rpx;
		padding: 20rpx 0;
	}

	.submit-btn:active {
		opacity: 0.8;
	}
</style>
